package kava.framework.lcdui;

import java.io.IOException;
import java.util.Hashtable;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import kava.framework.util.GUIUtil;
import kava.framework.util.Util;

public class Form {
	// 0x00ffffff:transparent
	public static final int COLOR_NONE = 0xffffffff;
	protected String title;
	protected int x;
	protected int y;
	protected int width;
	protected int height;

	protected int bgColor = COLOR_NONE;
	protected int fgColor;
	protected String bgImage;
	protected boolean isModal;
	protected long tick;
	protected int fontSize = Font.SIZE_SMALL;
	protected Hashtable pngRes;
	protected String pngResStr;

	protected boolean hasLoadRes;

	public Form() {
		x = 0;
		y = 0;
		width = KavaFrame.getScreenWidth();
		height = KavaFrame.getScreenHeight();
	}

	public void initFromScript(String uiFile, String id) {
		ScriptParser.parseFormFromScript(this, uiFile, id);
	}

	protected final void setRect(int x, int y, int width, int height) {
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
	}

	boolean isFullScreen() {
		return x == 0 && y == 0 && width == KavaFrame.getScreenWidth()
				&& height == KavaFrame.getScreenHeight();
	}

	public int getX() {
		return x;
	}

	public int getY() {
		return y;
	}

	public int getWidth() {
		return width;
	}

	public int getHeight() {
		return height;
	}

	public boolean isTheFrontest() {
		return KavaFrame.peek() == this;
	}

	public final void updateForm() {
		Graphics g = KavaFrame.getGraphics();
		if (g != null) {
			paint(g);
			KavaFrame.freeGraphics();
			KavaFrame.invalideRec(x, y, width, height);
		}

	}

	protected final void loadPngRes() {
		Util.debug("Form.loadPngRes", this.title + " loading pngs");
		if (this.pngRes == null) {
			this.pngRes = new Hashtable();
		}
		this.pngRes.clear();
		if (this.pngResStr != null) {
			String[] pngs = Util.split(this.pngResStr, ",");
			try {
				for (int i = 0; i < pngs.length; i++) {
					String png = pngs[i].trim();
					String path = "/images/" + png;
					// System.out.println("8888888888888 pngPath = "+ path);
					this.pngRes.put(png, Image.createImage(path));
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	protected final void freePngRes() {
		if (this.pngRes != null) {
			this.pngRes.clear();
			System.gc();
			Util.debug("Form.freePngRes", this.title + " free all pngs");
		}
	}

	public Image getPngFromPngRes(String pngFileName) {
		if (this.pngRes != null) {
			return (Image) this.pngRes.get(pngFileName);
		} else {
			Util.debug("Form.getPngFromPngRes", "can't load png = "
					+ pngFileName);
			return null;
		}
	}

	protected static final void darkleBg(Graphics g) {
		if (KavaFrame.numAlphaLevels() > 2) {

			int sw = KavaFrame.getScreenWidth();
			int sh = KavaFrame.getScreenHeight();
			final int totalBgImageCount = 4;
			int bgImageHeight = sh / totalBgImageCount;
			int[] bgImageData = new int[sw * bgImageHeight];
			for (int i = 0; i < bgImageData.length; i++) {
				bgImageData[i] = 0x87000000;
			}
			for (int i = 0; i < totalBgImageCount + 1; i++) {
				g.drawRGB(bgImageData, 0, sw, 0, bgImageHeight * i, sw,
						bgImageHeight, true);

			}

			bgImageData = null;
			System.gc();
			KavaFrame.invalideRec(0, 0, sw, sh);
		}

	}
	
	
	
	protected void drawBackGround(Graphics g) {
		if (this.bgColor != Form.COLOR_NONE) {
			g.setColor(this.bgColor);
			g.fillRect(x, y, width, height);
		}

		if (this.bgImage != null) {
			Image bgImage = getPngFromPngRes(this.bgImage);
			GUIUtil.drawImageCenter(g, bgImage, x, y, width, height);
		}
	}

	// the methods to be override
	protected void paint(Graphics g) {
		g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
				this.fontSize));
		drawBackGround(g);
	}

	protected void onActive() {
		if (hasLoadRes == false) {
			loadPngRes();
			hasLoadRes = true;
		}

	}

	protected void onDeactive() {
		freePngRes();
		hasLoadRes = false;
	}

	protected void onKeyPressed(int keyCode) {

	}

	protected void onKeyReleased(int keyCode) {

	}

	protected void onKeyRepeat(int keyCode) {

	}

	protected void onPenDown(int x, int y) {

	}

	protected void onPenUp(int x, int y) {

	}

	protected void onPenMove(int p1, int p2) {

	}

	// //////////////////////////////////////////////////////////////////////////////

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setX(int x) {
		this.x = x;
	}

	public void setY(int y) {
		this.y = y;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int getBgColor() {
		return bgColor;
	}

	public void setBgColor(int bgColor) {
		this.bgColor = bgColor;
	}

	public String getBgImage() {
		return bgImage;
	}

	public void setBgImage(String bgImage) {
		this.bgImage = bgImage;
	}

	public boolean isModal() {
		return isModal;
	}

	public void setModal(boolean isModal) {
		this.isModal = isModal;
	}

	public long getTick() {
		return tick;
	}

	public void setTick(long tk) {
		this.tick = tk;
	}

	public int getFgColor() {
		return fgColor;
	}

	public void setFgColor(int fgColor) {
		this.fgColor = fgColor;
	}

	public int getFontSize() {
		return fontSize;
	}

	public void setFontSize(int fontSize) {
		this.fontSize = fontSize;
	}

	public void setPngResStr(String pngResStr) {
		this.pngResStr = pngResStr;
	}

}
